package test.serverframe.armc.server.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.microcore.util.DateUtil;

public class DateConvertCronUtil {
    public static String xZero(String string){
        String result=null;
        if (string.startsWith("0")){
            result=string.substring(string.length()-1,string.length());

        }else {
            result=string;
        }

        return result;
    }
    public static String getCron(Date startTime, Date endTime, String cycle,int method) {
        String result=null;
        String startStrTime = DateUtil.formatDate(startTime);
        String endStrTime = DateUtil.formatDate(endTime);

        String sHour = xZero(startStrTime.substring(11, 13));
        String sMin = xZero(startStrTime.substring(14, 16));
        String sSecond = xZero(startStrTime.substring(17, 19));
        String sYear = startStrTime.substring(0, 4);
        String sMonth = xZero(startStrTime.substring(5, 7));
        String sDay = xZero(startStrTime.substring(8, 10));

        String eHour = xZero(endStrTime.substring(11, 13));
        String eMin = xZero(endStrTime.substring(14, 16));
        String eSecond = xZero(endStrTime.substring(17, 19));
        String eYear = endStrTime.substring(0, 4);
        String eMonth = xZero(endStrTime.substring(5, 7));
        String eDay = xZero(endStrTime.substring(8, 10));

        String day = null;
        String month = null;
        String year = null;
        String hour = null;
        String second = null;
        String min = null;

        if (sYear.equals(eYear)) {
            year = sYear;

        } else {
            year = sYear + "-" + eYear;

        }

        if (sMonth.equals(eMonth)) {
            month = sMonth;

        } else {
            month = sMonth + "-" + eMonth;

        }

        if (sDay.equals(eDay)) {
            day = sDay;

        } else {
            day = sDay + "-" + eDay;

        }
        if (sHour.equals(eHour)) {
            hour = sHour;

        } else {
            hour = sHour + "-" + eHour;

        }
        if (sMin.equals(eMin)) {
            min = sMin;

        } else {
            min = sMin + "-" + eMin;

        }
        if (sSecond.equals(eSecond)) {
            second = sSecond;

        } else {
            second = sSecond + "-" + eSecond;

        }
        /**
         * Cron的七个时间域互不相关,所以当一个时间域所剩下的时间数值不能被执行间隔所整除时,执行规则将无法达到预期
         */
        if (!"0".equals(sSecond)) {
            if ((60 - Integer.parseInt(cycle)) % Integer.parseInt(cycle) != 0) {
                result= "莫不是有问题";
            }

        }
        /**
         * 无限每天执行
         */
        if (method==2){
            result= second + "/" + cycle + " " + min + " " + hour+" "+"*"+" "+"*"+" "+"?";
        }

        /**
         * 周期执行 有结束时间
         */
        if (method == 1) {
            result=second + "/" + cycle + " " + min + " " + hour + " " + day + " " + month + " " + "?" + " " + year;
        }
        /**
         * 单次执行
         */
        if (method==0){
            result=sSecond+"/"+"59"+" "+sMin+" " +sHour+" "+sDay+" "+sMonth+" "+"?";
        }

        return result;
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(new Date());
        System.out.println(xZero("25"));
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date myDate1 = dateFormat.parse("2018-11-28 14:05:00");
        Date myDate2=dateFormat.parse("2018-12-05 15:30:00");

        System.out.println( getCron(myDate1,myDate2,"20",0));
    }

}